Page({
  data: {
    list: [],
    selectedLevel: '',
    selectedField: '',
    appointmentTime: ''
  },

  onLoad: function(options) {

    console.log(options);
    
    // 直接从options获取参数
    const levelName = options.levelName ? decodeURIComponent(options.levelName) : '';
    const categoryName = options.categoryName ? decodeURIComponent(options.categoryName) : '';
    const appointmentTime = options.appointmentTime ? decodeURIComponent(options.appointmentTime) : '';
    
    console.log("Decoded levelName:", levelName,"!!!!");
    console.log("Decoded categoryName:", categoryName,"!!!!");
    console.log("Decoded appointmentTime:", appointmentTime);
  
    // 设置初始值
    this.setData({
      selectedLevel: levelName,
      selectedField: categoryName,
      appointmentTime: appointmentTime
    });

    console.log(this.data.selectedLevel,this.data.selectedField,this.data.appointmentTime,"ajdfl;kajlk;");
  
    // 使用接收到的参数进行搜索
    if (levelName && categoryName && appointmentTime) {
      this.searchConsultants();
    } else {
      console.log("Missing required search parameters");
    }
  },

  searchConsultants: function() {
    console.log("Searching with params:", {
      teacherGradeName: this.data.selectedLevel,
      fieldName: this.data.selectedField,
      appointmentTime: this.data.appointmentTime
    });
  
    wx.request({
      url: 'http://localhost:8080/api/consultants/search',
      data: {
        teacherGradeName: this.data.selectedLevel,
        fieldName: this.data.selectedField,
        appointmentTime: this.data.appointmentTime
      },
      success: (res) => {
        console.log("Search results:", res.data);
        this.setData({
          list: res.data.consultants
        });
      },
      fail: (err) => {
        console.error('搜索失败:', err);
        wx.showToast({
          title: '搜索失败',
          icon: 'none'
        });
      }
    });
  },

  findxinxi: function(e) {
    const index = e.currentTarget.dataset.index;
    const consultant = this.data.list[index];
    
    // 跳转到咨询师详情页面
    wx.navigateTo({
      url: `/pages/index/reserve/consultantDetail/consultantDetail?id=${consultant.id}&appointmentTime=${this.data.appointmentTime}`,
    });
  }
});
